//logs.js
var util = require('../../utils/util.js');
var app = getApp();
var applet_park_api_url = app.globalData.applet_park_api_url;
var timeOut = 60;
Page({
  data: {
    tipsCode: "获取验证码",
    timeNum: 60,
    clikType: false,
    currentTab: 0
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  selectlogo: function (e) {
    var that = this;
    if (that.data.currentTab === e.currentTarget.dataset.index) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.index
      })
    }
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  // 点击获取手机验证码
  clickGainCode(e) {
    if (!this.data.clikType) {
      this.gainCode();
    }
  },
  //输入手机号获取验证码
  gainCode() {
    var that = this;
    if (this.data.mobile == '' || this.data.mobile == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
    } else {
      that.setData({
        clikType: true
      })
      if (that.data.timeNum < ttimeOut) {} else {
        wx.request({
          url: wx4agency_api_url + '/daka/getVerifiCode',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: "get",
          data: {
            mobile: this.data.mobile
          },
          success: function (res) {
            that.getTime();
            if (res.data.success) {} else {}
          }
        })
      }
    }
  },
  //获取验证码时候显示时间
  getTime() {
    var that = this;
    var timer = setInterval(function () {
      that.data.timeNum--;
      if (that.data.timeNum <= 0) {
        that.setData({
          tipsCode: "重新获取验证码",
          timeNum: timeOut,
          clikType: false
        });
        clearInterval(timer);
      } else {
        that.setData({
          tipsCode: "重新发送" + that.data.timeNum + "秒",
        })
      }
    }, 1000)
  },


  getPhoneNumber: function (e) {


    var that = this;
    var detail = e.detail;
    wx.checkSession({
      success: function (res) {

      },
      fail: function (res) {

        app.getOpenid();
      },
      complete() {

        if ("getPhoneNumber:ok" != detail.errMsg) {

          wx.showModal({
            title: '快捷登录失败',
            content: '快捷登录失败,若要获取更多功能请先登录',
            success(res) {
              if (res.confirm) {

                wx.navigateTo({
                  url: '../register/register',
                })
              } else if (res.cancel) {

                wx.showToast({
                  title: '获取用户信息失败',
                  icon: 'fail'
                });
                wx.switchTab({
                  url: '../user_center/user_center',
                });
              }
            },
            fail: function (res) {

            }
          })

          return;
        }

        var openId = wx.getStorageSync("OPENID");

        var SESSION_KEY = wx.getStorageSync('SESSION_KEY');



        if (!openId) {
          app.getOpenid();
        }
        if (openId) {

          wx.request({
            url: applet_park_api_url + '/common/wxDecrypt',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              "appId": app.globalData.appid,
              "sessionKey": wx.getStorageSync('SESSION_KEY'),
              "encryptedData": detail.encryptedData,
              "iv": detail.iv
            },
            success: function (res) {

              if (res.data.success) {

                var t = JSON.parse(res.data.data.res);

                wx.setStorageSync("phoneNumber", t.phoneNumber);


                that.register();
              } else {
                wx.showModal({
                  title: '获取用户手机号失败',
                  showCancel: false,
                  confirmText: '确认',
                  success(res) {},
                })
              }
            },
            fail: function () {
              wx.showModal({
                title: '获取用户手机号失败',
                showCancel: false,
                confirmText: '确认',
                success(res) {},
              })
            }
          })
        }
      }
    })
  },
  // 立即注册
  register: function () {

    var that = this;
    var userInfo = wx.getStorageSync("USER_INFO");
    var openId = wx.getStorageSync("OPENID");
    wx.request({
      url: applet_park_api_url + "/wxuser/registerNoAuth",
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openId,
        phone: wx.getStorageSync('phoneNumber'),
        nickname: userInfo.nickName,
        appId: app.globalData.appid
      },
      success: function (res) {
        if (res.data.success) {
          userInfo.bindPhone = wx.getStorageSync('phoneNumber');
          wx.setStorageSync("USER_INFO", userInfo);
          wx.switchTab({
            url: '../user_center/user_center',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: function (res) {}
    })

  },
  getUserInfo: function (res) {
    var userInfo = JSON.parse(res.detail.rawData);
    if (userInfo) {
      //将用户信息赋值给app.globalData.userInfo
      for (var pro in userInfo) {
        app.globalData.userInfo[pro] = userInfo[pro];
      }
      //将用户信息缓存下来
      wx.setStorageSync('USER_INFO', userInfo);
      //调用登录
      this.login();
    } else {
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'none'
      });
    }
  },
  login: function () {

  },
})